package ${modulePackage}.web;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import ${modulePackage}.model.${className};
import ${modulePackage}.service.${className}Service;
import ${modulePackage}.web.form.${className}SearchForm;
/**
 * ${codeName}控制器
 *
 */
@Controller
@RequestMapping("/control/${moduleSimplePackage}/${lowerName}Controller")
public class ${className}Controller extends BaseController {

	private static final Logger logger = Logger.getLogger(${className}Controller.class);
	
	/**
	 * ${codeName}服务类
	 */
	@Resource(name = "${lowerName}ServiceImpl")
	private ${className}Service ${lowerName}Service;
	
	/**
	 * 列出${codeName}
	 */
	@RequestMapping("/list")
	public String list(${className}SearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			
			//创建页面对象
			PageView<${className}> pageView = new PageView<${className}>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(${lowerName}Service.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示添加${codeName}
	 */
	@RequestMapping("/displayAdd")
	public String displayAdd(${className} ${lowerName}, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 添加${codeName}
	 */
	@RequestMapping("/add")
	public String add(${className} ${lowerName}, ModelMap map) {
		try {
			//TODO 设置${lowerName}  eg:主键信息
			
			${lowerName}Service.save(${lowerName});
			return list(new ${className}SearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示编辑${codeName}
	 */
	@RequestMapping("/displayEdit")
	public String displayEdit(${className}SearchForm form, ModelMap map) {
		try {
			//TODO 查询对象
			//eg:${className} ${lowerName} = ${lowerName}Service.find(form.getId());
			//eg:map.addAttribute("entity", ${lowerName});
			
			
			//TODO 转发页面
			return "";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 编辑${codeName}
	 */
	@RequestMapping("/edit")
	public String edit(${className} ${className}, ModelMap map) {
		try {
			//TODO:处理${codeName}
			
			
			${lowerName}Service.updateBySelective(${className});
			return list(new ${className}SearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}

	/**
	 * 删除单个${codeName}
	 */
	@RequestMapping("/delete${lowerName}")
	public String delete(${className}SearchForm form, ModelMap map) {
		try {
			//TODO  eg:${lowerName}Service.delete(form.getId());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 批量删除${codeName}
	 */
	@RequestMapping("/batchDelete")
	public String deletes(${className}SearchForm form, ModelMap map) {
		try {
//			TODO  eg:${lowerName}Service.batchDelete(form.getIds());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
}
